﻿using System;
using System.Web;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Threading;
using BrnMall.Core;
using BrnMall.Services;
using BrnMall.Web.Framework;
using BrnMall.Web.StoreAdmin.Models;
using BrnMall.Manager.Manager;
using BrnMall.Manager.Manager.User;
using System.Linq;
using BrnMall.Manager.Domain.Ship;
using NPOI.XSSF.UserModel;
using NPOI.SS.UserModel;
using System.Text;
using System.IO;
using NPOI.HSSF.UserModel;
using System.Data;

namespace BrnMall.Web.StoreAdmin.Controllers
{
    /// <summary>
    /// 店铺后台订单控制器类
    /// </summary>
    public partial class OrderController : BaseStoreAdminController
    {
        /// <summary>
        /// 订单列表
        /// </summary>
        /// <param name="osn">订单编号</param>
        /// <param name="accountName">账户名</param>
        /// <param name="consignee">收货人</param>
        /// <param name="orderState">订单状态</param>
        /// <param name="pageSize">每页数</param>
        /// <param name="pageNumber">当前页数</param>
        /// <returns></returns>
        public ActionResult OrderList(string osn, string accountName, string consignee, int orderState = 0, string sid = "", int pageSize = 15, int pageNumber = 1)
        {
            //获取用户id
            int uid = Users.GetUidByAccountName(accountName);

            string condition = AdminOrders.GetOrderListCondition(WorkContext.StoreId, osn, uid, consignee, orderState, sid);

            PageModel pageModel = new PageModel(pageSize, pageNumber, AdminOrders.GetOrderCount(condition));

            List<SelectListItem> orderStateList = new List<SelectListItem>();
            orderStateList.Add(new SelectListItem() { Text = "全部", Value = "0" });
            orderStateList.Add(new SelectListItem() { Text = "等待付款", Value = ((int)OrderState.WaitPaying).ToString() });
            orderStateList.Add(new SelectListItem() { Text = "待确认", Value = ((int)OrderState.Confirming).ToString() });
            orderStateList.Add(new SelectListItem() { Text = "已确认", Value = ((int)OrderState.Confirmed).ToString() });
            orderStateList.Add(new SelectListItem() { Text = "备货中", Value = ((int)OrderState.PreProducting).ToString() });
            orderStateList.Add(new SelectListItem() { Text = "已发货", Value = ((int)OrderState.Sended).ToString() });
            orderStateList.Add(new SelectListItem() { Text = "已收货", Value = ((int)OrderState.Received).ToString() });
            orderStateList.Add(new SelectListItem() { Text = "已锁定", Value = ((int)OrderState.Locked).ToString() });
            orderStateList.Add(new SelectListItem() { Text = "已取消", Value = ((int)OrderState.Cancelled).ToString() });

            var orderList = AdminOrders.GetOrderList(pageModel.PageSize, pageModel.PageNumber, condition);
            orderList.Columns.Add(new System.Data.DataColumn("Name", typeof(string)));
            orderList.Columns.Add(new System.Data.DataColumn("ShowImg", typeof(string)));
            foreach (System.Data.DataRow dr in orderList.Rows)
            {
                var productList = Orders.GetOrderProductList(Convert.ToInt32(dr["oid"]));
                if (productList.Count > 0)
                {
                    var product = productList[0];
                    dr["Name"] = product.Name;
                    dr["ShowImg"] = product.ShowImg;
                }
            }

            OrderListModel model = new OrderListModel()
            {
                OrderList = orderList,
                PageModel = pageModel,
                OSN = osn,
                AccountName = accountName,
                Consignee = consignee,
                OrderState = orderState,
                OrderStateList = orderStateList
            };
            MallUtils.SetAdminRefererCookie(string.Format("{0}?pageNumber={1}&pageSize={2}&OSN={3}&AccountName={4}&Consignee={5}&OrderState={6}",
                                                          Url.Action("orderlist"),
                                                          pageModel.PageNumber,
                                                          pageModel.PageSize,
                                                          osn, accountName, consignee, orderState));

            return View(model);
        }

        /// <summary>
        /// 订单信息
        /// </summary>
        /// <param name="oid">订单id</param>
        /// <returns></returns>
        public ActionResult OrderInfo(int oid = -1)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");

            OrderInfoModel model = new OrderInfoModel();
            model.OrderInfo = orderInfo;
            model.RegionInfo = Regions.GetRegionById(orderInfo.RegionId);
            model.UserInfo = Users.GetUserById(orderInfo.Uid);
            model.UserRankInfo = UserRanks.GetUserRankById(model.UserInfo.UserRid) ?? new UserRankInfo();
            model.OrderProductList = Orders.GetOrderProductList(oid);
            model.OrderActionList = OrderActions.GetOrderActionList(oid);

            ViewData["referer"] = MallUtils.GetStoreAdminRefererCookie();
            return View(model);
        }

        /// <summary>
        /// 更新订单配送费用
        /// </summary>
        [HttpGet]
        public ActionResult UpdateOrderShipFee(int oid = -1)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");
            if (!(orderInfo.OrderState == (int)OrderState.WaitPaying || (orderInfo.OrderState == (int)OrderState.Confirming && orderInfo.PayMode == 0)))
                return PromptView(Url.Action("orderinfo", new { oid = oid }), "不能修改订单配送费用");

            UpdateOrderShipFeeModel model = new UpdateOrderShipFeeModel();
            model.ShipFee = orderInfo.ShipFee;
            ViewData["orderInfo"] = orderInfo;
            return View(model);
        }

        /// <summary>
        /// 更新订单配送费用
        /// </summary>
        [HttpPost]
        public ActionResult UpdateOrderShipFee(UpdateOrderShipFeeModel model, int oid = -1)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");
            if (!(orderInfo.OrderState == (int)OrderState.WaitPaying || (orderInfo.OrderState == (int)OrderState.Confirming && orderInfo.PayMode == 0)))
                return PromptView(Url.Action("orderinfo", new { oid = oid }), "不能修改订单配送费用");

            if (ModelState.IsValid)
            {
                decimal change = model.ShipFee - orderInfo.ShipFee;
                Orders.UpdateOrderShipFee(orderInfo.Oid, model.ShipFee, orderInfo.OrderAmount + change, orderInfo.SurplusMoney + change);
                CreateOrderAction(oid, OrderActionType.UpdateShipFee, "您订单的配送费用已经修改");
                AddStoreAdminLog("更新订单配送费用", "更新订单配送费用,订单ID为:" + oid);

                if ((orderInfo.SurplusMoney + change) <= 0)
                    AdminOrders.UpdateOrderState(oid, OrderState.Confirming);

                return PromptView(Url.Action("orderinfo", new { oid = oid }), "更新订单配送费用成功");
            }

            ViewData["orderInfo"] = orderInfo;
            return View(model);
        }

        /// <summary>
        /// 更新订单折扣
        /// </summary>
        [HttpGet]
        public ActionResult UpdateOrderDiscount(int oid = -1)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");
            if (!(orderInfo.OrderState == (int)OrderState.WaitPaying || (orderInfo.OrderState == (int)OrderState.Confirming && orderInfo.PayMode == 0)))
                return PromptView(Url.Action("orderinfo", new { oid = oid }), "不能修改订单折扣");

            UpdateOrderDiscountModel model = new UpdateOrderDiscountModel();
            model.Discount = orderInfo.Discount;
            ViewData["orderInfo"] = orderInfo;
            return View(model);
        }

        /// <summary>
        /// 更新订单折扣
        /// </summary>
        [HttpPost]
        public ActionResult UpdateOrderDiscount(UpdateOrderDiscountModel model, int oid = -1)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");
            if (!(orderInfo.OrderState == (int)OrderState.WaitPaying || (orderInfo.OrderState == (int)OrderState.Confirming && orderInfo.PayMode == 0)))
                return PromptView(Url.Action("orderinfo", new { oid = oid }), "不能修改订单折扣");

            if (model.Discount > (orderInfo.SurplusMoney + orderInfo.Discount))
                ModelState.AddModelError("Discount", "折扣不能大于需支付金额");

            if (ModelState.IsValid)
            {
                decimal surplusMoney = orderInfo.SurplusMoney + orderInfo.Discount - model.Discount;
                Orders.UpdateOrderDiscount(orderInfo.Oid, model.Discount, surplusMoney);
                CreateOrderAction(oid, OrderActionType.UpdateDiscount, "您订单的折扣已经修改");
                AddStoreAdminLog("更新订单折扣", "更新订单折扣,订单ID为:" + oid);

                if (surplusMoney <= 0)
                    AdminOrders.UpdateOrderState(oid, OrderState.Confirming);

                return PromptView(Url.Action("orderinfo", new { oid = oid }), "更新订单折扣成功");
            }
            ViewData["orderInfo"] = orderInfo;
            return View(model);
        }

        /// <summary>
        /// 确认订单
        /// </summary>
        public ActionResult ConfirmOrder(int oid = -1)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");
            if (orderInfo.OrderState != (int)OrderState.Confirming)
                return PromptView(Url.Action("orderinfo", new { oid = oid }), "买家还未付款，不能确认订单");

            AdminOrders.ConfirmOrder(orderInfo);
            CreateOrderAction(oid, OrderActionType.Confirm, "您的订单已经确认,正在备货中");
            AddStoreAdminLog("确认订单", "确认订单,订单ID为:" + oid);
            return PromptView(Url.Action("orderinfo", new { oid = oid }), "确认订单成功");
        }

        /// <summary>
        /// 备货
        /// </summary>
        public ActionResult PreOrderProduct(int oid = -1)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");
            if (orderInfo.OrderState != (int)OrderState.Confirmed)
                return PromptView(Url.Action("orderinfo", new { oid = oid }), "订单还未确认，请先确认");

            AdminOrders.PreProduct(orderInfo);
            CreateOrderAction(oid, OrderActionType.PreProduct, "您的订单已经备货完成");
            AddStoreAdminLog("备货", "备货,订单ID为:" + oid);
            return PromptView(Url.Action("orderinfo", new { oid = oid }), "备货成功");
        }

        /// <summary>
        /// 发货
        /// </summary>
        [HttpGet]
        public ActionResult SendOrderProduct(int oid = -1)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");
            if (orderInfo.OrderState != (int)OrderState.PreProducting)
                return PromptView(Url.Action("orderinfo", new { oid = oid }), "订单还未完成备货,不能发货");

            SendOrderProductModel model = new SendOrderProductModel();

            Load(orderInfo);
            return View(model);
        }

        /// <summary>
        /// 发货
        /// </summary>
        [HttpPost]
        public ActionResult SendOrderProduct(SendOrderProductModel model, int oid = -1)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");
            if (orderInfo.OrderState != (int)OrderState.PreProducting)
                return PromptView(Url.Action("orderinfo", new { oid = oid }), "订单还未完成备货,不能发货");

            ShipCompanyInfo shipCompanyInfo = ShipCompanies.GetShipCompanyById(model.ShipCoId);
            if (shipCompanyInfo == null)
                ModelState.AddModelError("ShipCoId", "请选择配送公司");

            if (ModelState.IsValid)
            {
                //达达配送发货
                if (shipCompanyInfo.Name.Contains("达达"))
                {
                    var errMsg = Ships.AddOrder(orderInfo, WorkContext.Uid);
                    if (!string.IsNullOrEmpty(errMsg))
                    {
                        return PromptView(string.Concat("配送失败：", errMsg));
                    }

                    AdminOrders.SendOrder(oid, OrderState.Sended, model.ShipSN, model.ShipCoId, shipCompanyInfo.Name, DateTime.Now);
                    CreateOrderAction(oid, OrderActionType.Send, "您的订单已经发货,发货方式为:" + shipCompanyInfo.Name + "，等待骑手接单");
                }
                else
                {
                    //其他配送方式发货
                    AdminOrders.SendOrder(oid, OrderState.Sended, model.ShipSN, model.ShipCoId, shipCompanyInfo.Name, DateTime.Now);
                    CreateOrderAction(oid, OrderActionType.Send, "您的订单已经发货,发货方式为:" + shipCompanyInfo.Name + "，单号为：" + model.ShipSN);
                }

                AddStoreAdminLog("发货", "发货,订单ID为:" + oid);

                //发送消息
                var t = new Thread(() => Orders.SendDeliverSuccessMessage(oid));
                t.Start();
                return PromptView(Url.Action("orderinfo", new { oid = oid }), "发货成功");
            }
            Load(orderInfo);
            return View(model);
        }

        /// <summary>
        /// 锁定订单
        /// </summary>
        public ActionResult LockOrder(int oid = -1)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");
            if (!(orderInfo.OrderState == (int)OrderState.WaitPaying || (orderInfo.OrderState == (int)OrderState.Confirming && orderInfo.PayMode == 0)))
                return PromptView(Url.Action("orderinfo", new { oid = oid }), "订单当前不能锁定");

            AdminOrders.LockOrder(orderInfo);
            CreateOrderAction(oid, OrderActionType.Lock, "订单已锁定");
            AddStoreAdminLog("锁定", "锁定,订单ID为:" + oid);
            return PromptView(Url.Action("orderinfo", new { oid = oid }), "锁定成功");
        }

        /// <summary>
        /// 取消订单
        /// </summary>
        public ActionResult CancelOrder(int oid = -1)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");
            if (!(orderInfo.OrderState == (int)OrderState.WaitPaying || (orderInfo.OrderState == (int)OrderState.Confirming && orderInfo.PayMode == 0)))
                return PromptView(Url.Action("orderinfo", new { oid = oid }), "订单当前不能取消");

            PartUserInfo partUserInfo = Users.GetPartUserById(orderInfo.Uid);
            AdminOrders.CancelOrder(ref partUserInfo, orderInfo, WorkContext.Uid, DateTime.Now);
            CreateOrderAction(oid, OrderActionType.Cancel, "订单已取消");
            AddStoreAdminLog("取消订单", "取消订单,订单ID为:" + oid);
            return PromptView(Url.Action("orderinfo", new { oid = oid }), "取消订单成功");
        }

        /// <summary>
        /// 取消达达配送的订单
        /// </summary>
        /// <param name="oid"></param>
        /// <returns></returns>
        public ActionResult CancelOrderByShip(int oid)
        {
            MallUtils.SetAdminRefererCookie(Url.Action("CancelOrderByShip", new { oid = oid }));
            var reasonList = Ships.GetCacelReason();
            return View(reasonList);
        }

        /// <summary>
        /// 取消达达配送的订单
        /// </summary>
        /// <param name="oid"></param>
        /// <param name="reason_id"></param>
        /// <param name="cancel_reason"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CancelOrderByShip(int oid, int reason_id, string cancel_reason)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");
            if (!orderInfo.ShipCoName.Contains("达达"))
            {
                if (!(orderInfo.OrderState == (int)OrderState.WaitPaying || (orderInfo.OrderState == (int)OrderState.Confirming && orderInfo.PayMode == 0)))
                    return PromptView(Url.Action("orderinfo", new { oid = oid }), "订单当前不能取消");
            }

            //取消达达配送
            var errMsg = Ships.CacelOrder(orderInfo, reason_id, cancel_reason);
            if (errMsg != "")
                return PromptView(string.Concat("操作失败：", errMsg));

            PartUserInfo partUserInfo = Users.GetPartUserById(orderInfo.Uid);
            AdminOrders.CancelOrder(ref partUserInfo, orderInfo, WorkContext.Uid, DateTime.Now);
            CreateOrderAction(oid, OrderActionType.Cancel, "订单已取消");
            AddStoreAdminLog("取消订单", "取消订单,订单ID为:" + oid);
            return PromptView(Url.Action("orderinfo", new { oid = oid }), "取消订单成功");
        }

        /// <summary>
        /// 打印订单
        /// </summary>
        /// <param name="oid">订单id</param>
        /// <returns></returns>
        public ActionResult PrintOrder(int oid)
        {
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");

            PrintOrderModel model = new PrintOrderModel()
            {
                OrderInfo = orderInfo,
                RegionInfo = Regions.GetRegionById(orderInfo.RegionId),
                OrderProductList = AdminOrders.GetOrderProductList(oid),
            };

            return View(model);
        }

        private void Load(OrderInfo orderInfo)
        {
            ViewData["orderInfo"] = orderInfo;
            List<SelectListItem> shipCompanyList = new List<SelectListItem>();
            shipCompanyList.Add(new SelectListItem() { Text = "请选择", Value = "0" });
            foreach (ShipCompanyInfo shipCompanyInfo in ShipCompanies.GetShipCompanyList())
            {
                shipCompanyList.Add(new SelectListItem() { Text = shipCompanyInfo.Name, Value = shipCompanyInfo.ShipCoId.ToString() });
            }
            ViewData["shipCompanyList"] = shipCompanyList;
        }

        /// <summary>
        /// 创建订单行为
        /// </summary>
        private void CreateOrderAction(int oid, OrderActionType orderActionType, string actionDes)
        {
            OrderActions.CreateOrderAction(new OrderActionInfo()
            {
                Oid = oid,
                Uid = WorkContext.Uid,
                RealName = AdminUsers.GetUserDetailById(WorkContext.Uid).RealName,
                ActionType = (int)orderActionType,
                ActionTime = DateTime.Now,
                ActionDes = actionDes
            });
        }

        /// <summary>
        /// 订单退款列表
        /// </summary>
        /// <param name="osn">订单编号</param>
        /// <param name="pageSize">每页数</param>
        /// <param name="pageNumber">当前页数</param>
        /// <returns></returns>
        public ActionResult RefundList(string osn, int pageSize = 15, int pageNumber = 1)
        {
            string condition = AdminOrderRefunds.GetOrderRefundListCondition(WorkContext.StoreId, osn);
            PageModel pageModel = new PageModel(pageSize, pageNumber, AdminOrderRefunds.GetOrderRefundCount(condition));

            OrderRefundListModel model = new OrderRefundListModel()
            {
                PageModel = pageModel,
                OrderRefundList = AdminOrderRefunds.GetOrderRefundList(pageModel.PageSize, pageModel.PageNumber, condition),
                OSN = osn
            };
            MallUtils.SetAdminRefererCookie(string.Format("{0}?pageNumber={1}&pageSize={2}&OSN={3}",
                                                          Url.Action("refundlist"),
                                                          pageModel.PageNumber,
                                                          pageModel.PageSize, osn));

            return View(model);
        }

        /// <summary>
        /// 订单售后服务列表
        /// </summary>
        /// <param name="accountName">账户名</param>
        /// <param name="applyStartTime">申请开始时间</param>
        /// <param name="applyEndTime">申请结束时间</param>
        /// <param name="oid">订单id</param>
        /// <param name="state">状态</param>
        /// <param name="type">类型</param>
        /// <param name="pageSize">每页数</param>
        /// <param name="pageNumber">当前页数</param>
        /// <returns></returns>
        public ActionResult AfterServiceList(string accountName, string applyStartTime, string applyEndTime, int oid = 0, int state = -1, int type = -1, int pageSize = 15, int pageNumber = 1)
        {
            //获取用户id
            int uid = Users.GetUidByAccountName(accountName);

            string condition = AdminOrderAfterServices.GetOrderProductAfterServiceListCondition(WorkContext.StoreId, uid, oid, state, type, applyStartTime, applyEndTime);

            PageModel pageModel = new PageModel(pageSize, pageNumber, AdminOrderAfterServices.GetOrderProductAfterServiceCount(condition));

            List<SelectListItem> stateList = new List<SelectListItem>();
            stateList.Add(new SelectListItem() { Text = "全部", Value = "-1" });
            stateList.Add(new SelectListItem() { Text = "审核中", Value = ((int)OrderAfterServiceState.Checking).ToString() });
            stateList.Add(new SelectListItem() { Text = "审核通过", Value = ((int)OrderAfterServiceState.CheckAgree).ToString() });
            stateList.Add(new SelectListItem() { Text = "审核拒绝", Value = ((int)OrderAfterServiceState.CheckRefuse).ToString() });
            stateList.Add(new SelectListItem() { Text = "客户已邮寄", Value = ((int)OrderAfterServiceState.Sended).ToString() });
            stateList.Add(new SelectListItem() { Text = "商城已收货", Value = ((int)OrderAfterServiceState.Received).ToString() });
            stateList.Add(new SelectListItem() { Text = "商城已发货", Value = ((int)OrderAfterServiceState.Backed).ToString() });
            stateList.Add(new SelectListItem() { Text = "完成", Value = ((int)OrderAfterServiceState.Completed).ToString() });

            List<SelectListItem> typeList = new List<SelectListItem>();
            typeList.Add(new SelectListItem() { Text = "全部", Value = "-1" });
            typeList.Add(new SelectListItem() { Text = "退货", Value = "0" });
            typeList.Add(new SelectListItem() { Text = "换货", Value = "1" });
            typeList.Add(new SelectListItem() { Text = "维修", Value = "2" });

            OrderAfterServiceListModel model = new OrderAfterServiceListModel()
            {
                PageModel = pageModel,
                OrderAfterServiceList = AdminOrderAfterServices.GetOrderProductAfterServiceList(pageModel.PageSize, pageModel.PageNumber, condition),
                Oid = oid,
                AccountName = accountName,
                State = state,
                StateList = stateList,
                Type = type,
                TypeList = typeList,
                ApplyStartTime = applyStartTime,
                ApplyEndTime = applyEndTime
            };
            MallUtils.SetAdminRefererCookie(string.Format("{0}?pageNumber={1}&pageSize={2}&oid={3}&AccountName={4}&state={5}&type={6}&applyStartTime={7}&applyEndTime={8}",
                                                          Url.Action("afterservicelist"),
                                                          pageModel.PageNumber, pageModel.PageSize,
                                                          oid, accountName, state, type, applyStartTime, applyEndTime));

            return View(model);
        }

        /// <summary>
        /// 订单售后服务信息
        /// </summary>
        /// <param name="asId">售后服务id</param>
        /// <returns></returns>
        public ActionResult OrderAfterServiceInfo(int asId)
        {
            OrderAfterServiceInfo orderAfterServiceInfo = AdminOrderAfterServices.GetOrderAfterServiceByASId(asId);
            if (orderAfterServiceInfo == null || orderAfterServiceInfo.StoreId != WorkContext.StoreId)
                return PromptView("订单售后服务不存在");

            OrderAfterServiceModel model = new OrderAfterServiceModel();
            model.OrderAfterServiceInfo = orderAfterServiceInfo;
            model.RegionInfo = Regions.GetRegionById(orderAfterServiceInfo.RegionId);

            ViewData["referer"] = MallUtils.GetMallAdminRefererCookie();
            return View(model);
        }

        /// <summary>
        /// 审核订单售后服务
        /// </summary>
        [HttpGet]
        public ActionResult CheckOrderAfterService(int asId)
        {
            OrderAfterServiceInfo orderAfterServiceInfo = AdminOrderAfterServices.GetOrderAfterServiceByASId(asId);
            if (orderAfterServiceInfo == null || orderAfterServiceInfo.StoreId != WorkContext.StoreId)
                return PromptView("订单售后服务不存在");
            if (orderAfterServiceInfo.State != (int)OrderAfterServiceState.Checking)
                return PromptView("不能执行此操作");

            CheckOrderAfterServiceModel model = new CheckOrderAfterServiceModel();
            ViewData["asId"] = asId;
            return View(model);
        }

        /// <summary>
        /// 审核订单售后服务
        /// </summary>
        [HttpPost]
        public ActionResult CheckOrderAfterService(int asId, CheckOrderAfterServiceModel model)
        {
            OrderAfterServiceInfo orderAfterServiceInfo = AdminOrderAfterServices.GetOrderAfterServiceByASId(asId);
            if (orderAfterServiceInfo == null || orderAfterServiceInfo.StoreId != WorkContext.StoreId)
                return PromptView("订单售后服务不存在");
            if (orderAfterServiceInfo.State != (int)OrderAfterServiceState.Checking)
                return PromptView("不能执行此操作");

            if (ModelState.IsValid)
            {
                if (model.State == 0)
                    AdminOrderAfterServices.CheckRefuseOrderAfterService(asId, model.CheckResult ?? "", DateTime.Now);
                else
                    AdminOrderAfterServices.CheckAgreeOrderAfterService(asId, model.CheckResult ?? "", DateTime.Now);
                return PromptView("操作成功");
            }
            ViewData["asId"] = asId;
            return View(model);
        }

        /// <summary>
        /// 商城收到客户邮寄的商品
        /// </summary>
        public ActionResult ReceiveOrderAfterService(int asId)
        {
            OrderAfterServiceInfo orderAfterServiceInfo = AdminOrderAfterServices.GetOrderAfterServiceByASId(asId);
            if (orderAfterServiceInfo == null || orderAfterServiceInfo.StoreId != WorkContext.StoreId)
                return PromptView("订单售后服务不存在");
            if (orderAfterServiceInfo.State != (int)OrderAfterServiceState.Sended)
                return PromptView("不能执行此操作");

            if (orderAfterServiceInfo.Type == 0)
            {
                AdminOrderAfterServices.CompleteOrderAfterService(asId);
                OrderInfo orderInfo = AdminOrders.GetOrderByOid(orderAfterServiceInfo.Oid);
                OrderRefunds.ApplyRefund(new OrderRefundInfo()
                {
                    State = (int)OrderRefundState.Applied,
                    StoreId = orderInfo.StoreId,
                    StoreName = orderInfo.StoreName,
                    Oid = orderInfo.Oid,
                    OSN = orderInfo.OSN,
                    Uid = orderInfo.Uid,
                    ASId = orderAfterServiceInfo.ASId,
                    PaySystemName = orderInfo.PaySystemName,
                    PayFriendName = orderInfo.PayFriendName,
                    PaySN = orderInfo.PaySN,
                    PayMoney = orderAfterServiceInfo.Money,
                    RefundMoney = orderAfterServiceInfo.Money,
                    ApplyTime = DateTime.Now
                });
            }
            else
            {
                AdminOrderAfterServices.ReceiveOrderAfterService(asId, DateTime.Now);
            }
            return PromptView("操作成功");
        }

        /// <summary>
        /// 邮寄给客户
        /// </summary>
        [HttpGet]
        public ActionResult BackOrderAfterService(int asId)
        {
            OrderAfterServiceInfo orderAfterServiceInfo = AdminOrderAfterServices.GetOrderAfterServiceByASId(asId);
            if (orderAfterServiceInfo == null || orderAfterServiceInfo.StoreId != WorkContext.StoreId)
                return PromptView("订单售后服务不存在");
            if (orderAfterServiceInfo.State != (int)OrderAfterServiceState.Received)
                return PromptView("不能执行此操作");

            BackOrderAfterServiceModel model = new BackOrderAfterServiceModel();

            ViewData["asId"] = asId;
            ViewData["orderAfterServiceInfo"] = orderAfterServiceInfo;
            List<SelectListItem> shipCompanyList = new List<SelectListItem>();
            shipCompanyList.Add(new SelectListItem() { Text = "请选择", Value = "0" });
            foreach (ShipCompanyInfo item in ShipCompanies.GetShipCompanyList())
            {
                shipCompanyList.Add(new SelectListItem() { Text = item.Name, Value = item.ShipCoId.ToString() });
            }
            ViewData["shipCompanyList"] = shipCompanyList;
            return View(model);
        }

        /// <summary>
        /// 邮寄给客户
        /// </summary>
        [HttpPost]
        public ActionResult BackOrderAfterService(int asId, BackOrderAfterServiceModel model)
        {
            OrderAfterServiceInfo orderAfterServiceInfo = AdminOrderAfterServices.GetOrderAfterServiceByASId(asId);
            if (orderAfterServiceInfo == null || orderAfterServiceInfo.StoreId != WorkContext.StoreId)
                return PromptView("订单售后服务不存在");
            if (orderAfterServiceInfo.State != (int)OrderAfterServiceState.Received)
                return PromptView("不能执行此操作");

            ShipCompanyInfo shipCompanyInfo = ShipCompanies.GetShipCompanyById(model.ShipCoId);
            if (shipCompanyInfo == null)
                ModelState.AddModelError("ShipCoId", "配送公司不存在");

            if (ModelState.IsValid)
            {
                AdminOrderAfterServices.BackOrderAfterService(asId, model.ShipCoId, shipCompanyInfo.Name, model.ShipSN);
                return PromptView("操作成功");
            }

            ViewData["asId"] = asId;
            ViewData["orderAfterServiceInfo"] = orderAfterServiceInfo;
            ViewData["orderAfterServiceInfo"] = orderAfterServiceInfo;
            List<SelectListItem> shipCompanyList = new List<SelectListItem>();
            shipCompanyList.Add(new SelectListItem() { Text = "请选择", Value = "0" });
            foreach (ShipCompanyInfo item in ShipCompanies.GetShipCompanyList())
            {
                shipCompanyList.Add(new SelectListItem() { Text = item.Name, Value = item.ShipCoId.ToString() });
            }
            ViewData["shipCompanyList"] = shipCompanyList;
            return View(model);
        }

        public ActionResult PrintConfig(int oid = -1)
        {
            //获取打印机的设置IP
            ViewData["JSIP"] = PrintConfigs.GetPrintConfigIP(WorkContext.StoreId);
            OrderInfo orderInfo = AdminOrders.GetOrderByOid(oid);
            if (orderInfo == null)
                return PromptView("订单不存在");
            if (orderInfo.StoreId != WorkContext.StoreId)
                return PromptView("不能操作其它店铺的订单");
            PrintOrderModel model = new PrintOrderModel()
            {
                OrderInfo = orderInfo,
                RegionInfo = Regions.GetRegionById(orderInfo.RegionId),
                OrderProductList = AdminOrders.GetOrderProductList(oid),
                PrintConfigList = PrintConfigs.GetPrintByShopID(orderInfo.StoreId),//增加打印
            };

            return View(model);
        }

        /// <summary>
        /// 打印列表视图
        /// </summary>
        /// <returns></returns>
        public ActionResult PrintSettingList()
        {
            List<PrintConfig> lst = PrintConfigs.GetPrintList(WorkContext.StoreId);
            if (lst != null)
                ViewData["PrintConfigList"] = lst;
            else
                ViewData["PrintConfigList"] = new List<PrintConfig>();
            ViewData["JSIP"] = PrintConfigs.GetPrintConfigIP(WorkContext.StoreId);
            return View();
        }

        /// <summary>
        /// 添加视图
        /// </summary>
        /// <returns></returns>
        public ActionResult AddPrintConfig()
        {
            string backUrl = MallUtils.GetStoreAdminRefererCookie();
            if (backUrl.Length == 0 || backUrl == "/storeadmin/order")
            {
                backUrl = Url.Action("printsettinglist");
                MallUtils.SetAdminRefererCookie(backUrl);
            }
            ViewData["referer"] = backUrl;
            ViewData["JSIP"] = PrintConfigs.GetPrintConfigIP(WorkContext.StoreId);
            return View();
        }

        /// <summary>
        /// 打印添加
        /// </summary>
        /// <returns></returns>
        public ActionResult PrintConfig_Add()
        {
            PrintConfig pc = new PrintConfig();
            pc.StoreID = WorkContext.StoreId;
            pc.PrintID = Convert.ToInt32(Request.Form["PrintID"]);
            pc.PrintFormat = Convert.ToInt32(Request.Form["PrintFormat"]);
            if (!PrintConfigs.IsExistsPrintConfig(pc))
            {
                if (PrintConfigs.InsertPrintConfig(pc) > 0)
                    return PromptView("/storeadmin/order/printsettinglist", "添加成功");
                else
                    return PromptView("/storeadmin/order/AddPrintConfig", "添加失败");
            }
            else
                return PromptView("/storeadmin/order/printsettinglist", "该记录已存在");
        }

        /// <summary>
        /// 打印删除
        /// </summary>
        /// <returns></returns>
        public ActionResult PrintConfig_Delete()
        {
            PrintConfig pc = new PrintConfig();
            pc.StoreID = WorkContext.StoreId;
            pc.PrintID = Convert.ToInt32(Request["PrintID"]);
            pc.PrintFormat = Convert.ToInt32(Request["PrintFormat"]);
            if (PrintConfigs.DeletePrintConfig(pc) > 0)
                return PromptView("/storeadmin/order/printsettinglist", "删除成功");
            else
                return PromptView("/storeadmin/order/printsettinglist", "删除失败");
        }

        /// <summary>
        /// 自动打印
        /// </summary>
        /// <param name="osn"></param>
        /// <param name="accountName"></param>
        /// <param name="consignee"></param>
        /// <param name="orderState"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageNumber"></param>
        /// <returns></returns>
        public ActionResult AutoPrintList()
        {
            //获取用户id
            int oid = PrintRecords.GetPrintOid(WorkContext.StoreId);
            PrintRecord pr = new PrintRecord();
            pr.StoreID = WorkContext.StoreId;
            pr.Oid = oid;
            pr.PrintState = 1;
            pr.PrintID = 0;
            PrintRecords.InsertPrintRecord(pr);
            ViewData["OidList"] = oid;
            return View();
        }

        [HttpGet]
        public void ErrorPrintResult()
        {
            int oid = Convert.ToInt32(Request["oid"]);
            PrintRecord pr = new PrintRecord();
            pr.StoreID = WorkContext.StoreId;
            pr.Oid = oid;
            pr.PrintState = 0;
            pr.PrintID = 0;
            PrintRecords.UpdatePrintRecord(pr);
        }

        public ActionResult PrintIPSetting()
        {
            string backUrl = MallUtils.GetStoreAdminRefererCookie();
            if (backUrl.Length == 0 || backUrl == "/storeadmin/order")
            {
                backUrl = Url.Action("printsettinglist");
                MallUtils.SetAdminRefererCookie(backUrl);
            }
            ViewData["referer"] = backUrl;
            ViewData["JSIP"] = PrintConfigs.GetPrintConfigIP(WorkContext.StoreId);
            return View();
        }

        public ActionResult PrintIPSetting2()
        {
            if (PrintConfigs.UpdatePrintConfigIP(WorkContext.StoreId, Request.Form["JSIP"].ToString()) > 0)
                return PromptView("/storeadmin/order/printsettinglist", "修改成功");
            else
                return PromptView("/storeadmin/order/printsettinglist", "修改失败");
        }

        public ActionResult DirectPayOrderList(string psn, string accountName, int pageSize = 15, int pageNumber = 1)
        {
            int uid = Users.GetUidByAccountName(accountName);
            var condition = $" 1=1 and storeid={WorkContext.StoreId}";
            if (uid > 0)
                condition += " and uid=" + uid;
            var result = DirectPaymentManager.GetStorePage(condition, pageNumber, pageSize);
            PageModel pageModel = new PageModel(pageSize, pageNumber, (int)result.TotalItems);
            DirectPaymentListModel model = new DirectPaymentListModel()
            {
                OrderList = result.Items.Select(t => new DirectPaymentModel
                {
                    AddTime = t.AddTime,
                    CouponMoney = t.CouponMoney,
                    OrderAmount = t.OrderAmount,
                    OrderState = t.OrderState,
                    PayCash = t.PayCash,
                    PayCreditCount = t.PayCreditCount,
                    PayCreditMoney = t.PayCreditMoney,
                    PaySN = t.PaySN,
                    PaySystemName = t.PaySystemName,
                    PayTime = t.PayTime,
                    Pid = t.Pid,
                    PSN = t.PSN,
                    StoreId = t.StoreId,
                    SurplusMoney = t.SurplusMoney,
                    User = Users.GetPartUserById(t.Uid)
                }).ToList(),
                PageModel = pageModel,
                PSN = psn,
                AccountName = accountName,
            };
            MallUtils.SetAdminRefererCookie(
                $"{Url.Action("DirectPayOrderList")}?pageNumber={pageModel.PageNumber}&pageSize={pageModel.PageSize}&PSN={psn}&AccountName={accountName}");
            return View(model);
        }
        [HttpGet]
        /// <summary>
        /// 导出订单
        /// </summary>
        /// <param name="export"></param>
        /// <returns></returns>
        public ActionResult Exportorder()
        {
            //string backUrl = MallUtils.GetStoreAdminRefererCookie();
            //if (backUrl.Length == 0 || backUrl == "/storeadmin/home/storeruninfo")
            //{
            //    backUrl = Url.Action("orderlist");
            //    MallUtils.SetAdminRefererCookie(backUrl);
            //}
            //ViewData["referer"] = backUrl;
            string file = Request.MapPath("/admin_store/export/订单明细表.xlsx");
            //获取订单表
            var dt = AdminOrders.GetAllOrderList(WorkContext.StoreId);
            IWorkbook workbook;
            string fileExt = Path.GetExtension(file).ToLower();
            if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(); } else { workbook = null; }
            //if (workbook == null) { return; }
            ISheet sheet = string.IsNullOrEmpty(dt.TableName) ? workbook.CreateSheet("Sheet1") : workbook.CreateSheet(dt.TableName);

            //表头  
            IRow row = sheet.CreateRow(0);
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                ICell cell = row.CreateCell(i);
                cell.SetCellValue(dt.Columns[i].ColumnName);
            }

            //数据  
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                IRow row1 = sheet.CreateRow(i + 1);
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    ICell cell = row1.CreateCell(j);
                        if (j == 3)
                        {
                            cell.SetCellValue(dt.Rows[i][j].ToString());
                            var orderstate = cell.StringCellValue;
                            var ordervalues = "";
                            switch (orderstate)
                            {
                                case "20":
                                    ordervalues = "等待付款";
                                    cell.SetCellValue(ordervalues);
                                    break;
                                case "40":
                                    ordervalues = "待确认";
                                    cell.SetCellValue(ordervalues);
                                    break;
                                case "60":
                                    ordervalues = "已确认";
                                    cell.SetCellValue(ordervalues);
                                    break;
                                case "80":
                                    ordervalues = "备货中";
                                    cell.SetCellValue(ordervalues);
                                    break;
                                case "100":
                                    ordervalues = "已发货";
                                    cell.SetCellValue(ordervalues);
                                    break;
                                case "120":
                                    ordervalues = "已收货";
                                    cell.SetCellValue(ordervalues);
                                    break;
                                case "140":
                                    ordervalues = "取消";
                                    cell.SetCellValue(ordervalues);
                                    break;
                                case "160":
                                    ordervalues = "锁定";
                                    cell.SetCellValue(ordervalues);
                                    break;
                                default:
                                    ordervalues = "未知状态";
                                    cell.SetCellValue(ordervalues);
                                    break;
                            }
                        } //else if (j ==18)
                        //{
                        //    cell.SetCellValue(dt.Rows[i][j].ToString());
                        //    var pagestate = cell.StringCellValue;
                        //    if (pagestate == "1")
                        //    {
                        //        cell.SetCellValue("在线付款");
                        //    } else { cell.SetCellValue("货到付款"); }

                        //} 
                        else if (j==19) {
                            cell.SetCellValue(dt.Rows[i][j].ToString());
                            var sendtime= cell.StringCellValue;
                            if(sendtime== "1900/1/1 星期一 0:00:00")
                            { cell.SetCellValue("未配送"); }
                        } else {
                            cell.SetCellValue(dt.Rows[i][j].ToString());
                        }
                       
                    }
            }

            //转为字节数组  
            MemoryStream stream = new MemoryStream();
            workbook.Write(stream);
            var buf = stream.ToArray();

            //保存为Excel文件  
            using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
            {
                fs.Write(buf, 0, buf.Length);
                fs.Flush();
            }
            //下载
            string filename = "订单明细表.xlsx";
            return File(Request.MapPath("/admin_store/export") + "\\" + filename, "application", filename);


        }


    }
}